// pages/address/address.js
import { userBehavior } from '@/miniprogram/utils/behaviors/userBehavior'
import { getstorage, setstorage, removeStorage } from '@/miniprogram/utils/storage.js'
import { deleteAddress, addressQuery, changeDefaultaddr } from '@/miniprogram/pre_loadModules/addressModule/utils/Memberaddress'

Page({
   behaviors: [userBehavior],

    /**
     * 页面的初始数据
     */
    data: {
        addressList: null,
        addDate: null,
        addrId: null,
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let { orgin } = options;
        this.updateUserInfo()
        if (orgin == "orderConfirm") {
            setstorage("orderconfirm",orgin)
        }
    },
    //订单页选则地址
    orderconfirmgetAddress(e) {
        let orgin = getstorage("orderconfirm")
        let { id } = e.currentTarget.dataset
        let that = this
        let {addressList} = this.data
        console.log("读出的orgin:",orgin);
        if ( orgin != "orderConfirm") {
            return
        }else {
            let changebuff = addressList.filter(res => {
                if (res.id == id) {
                    res.defaultStatus = 1
                    return res;
                } else {
                    res.defaultStatus = 0
                    return res;
                }
            })
            that.changeDefaultaddrfun(changebuff)
        }
        removeStorage('orderconfirm')
        wx.redirectTo({
            url: '/miniprogram/pre_loadModules/orderModule/pages/orderCheck/orderCheck',
        })
    },
    insertAddressBtn() {
        wx.navigateTo({
            url: '/miniprogram/pre_loadModules/addressModule/pages/insertOrUpdateaddress/insertOrUpdateaddress',
        })
        // wx.navigateTo({ url: 'plugin://address-form/index' });
    },
    //修改默认地址
    bindradioGroup(e) {
        let { addressList } = this.data;
        let id = e.detail.value
        let that = this
        let changebuff = addressList.filter(res => {
            if (res.id == id) {
                res.defaultStatus = 1
                return res;
            } else {
                res.defaultStatus = 0
                return res;
            }
        })
        wx.showModal({
            title: "更改提示",
            content: "您确定设置为默认地址吗？",
            success(res) {
                if (res.confirm) {
                    that.changeDefaultaddrfun(changebuff)
                } else {
                    wx.showToast({
                        title: '已取消！',
                    })
                    that.getaddressList()
                }
            }
        })
    },
    //更改地址信息
    updateInfoTipBtn(e) {
        let { id } = e.currentTarget.dataset
        wx.showModal({
            title: "修改提示",
            content: "您确定修改地址吗？",
            success(res) {
                if (res.confirm) {
                    wx.navigateTo({
                        url: `/miniprogram/pre_loadModules/addressModule/pages/insertOrUpdateaddress/insertOrUpdateaddress?id=${id}`,
                    })
                } else {
                    wx.showToast({
                        title: '已取消修改！',
                        icon: 'error'
                    })
                }
            }
        })

    },

    deleteAddressbtn(e) {
        var that = this;
        let { id } = e.currentTarget.dataset
        let sendData = {}
        sendData.id = id
        wx.showModal({
            title: "删除确认",
            content: "您确定删除这条地址吗？",
            success(res) {
                if (res.confirm) {
                    that.deleteUserAddress(sendData)
                } else {
                    wx.showToast({
                        title: '已取消',
                        icon: 'error'
                    })
                }
            }
        })

    },

    async deleteUserAddress(data) {
        let res = await deleteAddress(data)
        if (res.data.code == 200) {
            wx.showToast({
                title: '删除成功！',
            })
            this.getaddressList()
        }
    },

    async changeDefaultaddrfun(data) {
        let res = await changeDefaultaddr(data)
        if (res.data.code == 200) {
            wx.showToast({
                title: '修改成功',
            })
            this.getaddressList()
        }
    },
    addressOrderLogin() {
        wx.navigateTo({
            url: '/pages/login/login',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       this.updateUserInfo()
      this.getaddressList()
    },
    async getaddressList() {
       let {userInfo} = this.data
        let res = await addressQuery(userInfo.id)
        if (res.data.code == 200) {
            let addressList = res.data.data
            for (let i = 0; i < addressList.length; i++) {
               let s1 = addressList[i].phone.substring(0,3)
               let s2 = addressList[i].phone.substring(7,11)
               addressList[i].phone = s1 + "****" + s2 ;  
            }
            this.setData({
                addressList
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }

})